package cfar.actors.distributed;

import cfar.actors.Message;
import cfar.actors.Node;
import cfar.ff.data.Assocs.Assoc2;
import cfar.ff.data.Stream;
import cfar.ff.fun.F1;
import cfar.ff.interfaces.Sequence;
import cfar.ff.data.List;
import java.io.File;
import static cfar.utils.IO.*;
import static cfar.ff.fun.Curry.*;

import java.io.IOException;
import java.net.InetAddress;

/**
 *
 * @author badi
 */
public class Main {

    final static F1<Integer, Integer> f = new F1<Integer, Integer>() {

        public Integer f(Integer a) {
            return a + 10;
        }
    };

    public static void main(String[] args) throws IOException, InterruptedException {

        if (args.length == 1) {
            File sync = new File(args[0]);
            if (!sync.isDirectory()) {
                sync.mkdir();
            }

            new Thread(new ActorServer( sync.getAbsolutePath(), "cfs")).start();
        } else if (args.length == 2 && args[1].equalsIgnoreCase("client")) {

            Sequence<List,Assoc2<InetAddress, Integer>> as = available_hosts(args[0], "cfs");
            List<Node> nodes = List.nil();
            int i = 0;
            for (Assoc2<InetAddress, Integer> location : as) {
                Node n = new Node(location.$1, location.$2);
                n.tell(new Message(Signal.WorkF0, curry(f, i++)));
                nodes = nodes.cons(n);
                if (i > 10)
                    break;
            }

            for (Node n : nodes) {
                Assoc2 r = (Assoc2) n.call().payload;
                System.out.println(r.$1 + " ==> " + r.$2);
                n.tell(Message.stop);
            }
        }
        else {
            System.out.println("Usage: <synch_dir> | <synch_dir> client");
        }

    }
}
